package com.chain.BrotherExchange.ui.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;

import com.chain.BrotherExchange.C;
import com.chain.BrotherExchange.R;
import com.chain.BrotherExchange.data.model.Quotation;
import com.chain.BrotherExchange.ui.activity.VisualQuotationActivity;
import com.chain.BrotherExchange.ui.adapter.QuotationListAdapter;
import com.chain.BrotherExchange.ui.core.AbsNetworkStateFragment;
import com.chain.BrotherExchange.utils.FirstItemMarginDecoration;
import com.chain.BrotherExchange.viewmodel.QuotationViewModel;
import com.chain.BrotherExchange.viewmodel.core.ViewModelProviders;

import butterknife.Bind;


/**
 * 具体货币的行情
 */
public class QuotationListFragment extends AbsNetworkStateFragment {

    @Bind(R.id.recycler_view) RecyclerView mRecyclerView;
    @Bind(R.id.swipe_refresh_layout) SwipeRefreshLayout mSwipeRefreshLayout;

    private int mQuotationType;
    private QuotationViewModel mViewModel;
    private QuotationListAdapter mAdapter;

    @Override
    protected int getLayoutID() {
        return R.layout.simple_refresh_recycler_view;
    }

    @Override
    protected void initActions() {
        // 刷新样式
        mSwipeRefreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorPrimary));

        // 数据模型
        mViewModel = ViewModelProviders.of(this).get(QuotationViewModel.class);

        // 列表
        mAdapter = new QuotationListAdapter();
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new FirstItemMarginDecoration());

        // 请求行情数据
        mQuotationType = getArguments().getInt(C.EXTRA_PAGE_TYPE);
        requestQuotationInfo();

        // 刷新行情数据
        mSwipeRefreshLayout.setOnRefreshListener(this::requestQuotationInfo);

        // 跳转图表
        mAdapter.setItemClickListener((parent, view, position, id) -> {
            Quotation.DataBean.ListBean item = mAdapter.getItem(position);
            VisualQuotationActivity.launch(getContext(), item.getSymbol());
        });
    }

    private void requestQuotationInfo() {
        mViewModel.quotationInfo(mQuotationType).observe(this, resource -> {
            switch (resource.state) {
                case LOADING:
                    mSwipeRefreshLayout.setRefreshing(true);
                    break;

                case EMPTY:
                    mSwipeRefreshLayout.setRefreshing(false);
                    emptyNetwork();
                    break;

                case NOT_NETWORK:
                    mSwipeRefreshLayout.setRefreshing(false);
                    notNetwork();
                    break;

                case ERROR:
                    mSwipeRefreshLayout.setRefreshing(false);
                    errorNetwork(resource.message);
                    break;

                case SUCCESS:
                    mSwipeRefreshLayout.setRefreshing(false);
                    successNetwork();
                    mAdapter.setQuotationList(resource.source);
                    break;
            }
        });
    }

    @Override
    public void onAnewRequest() {
        requestQuotationInfo();
    }

    public static Fragment newInstance(int type) {
        Bundle bundle = new Bundle();
        bundle.putInt(C.EXTRA_PAGE_TYPE, type);
        QuotationListFragment fragment = new QuotationListFragment();
        fragment.setArguments(bundle);
        return fragment;
    }
}
